﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 职工端
{
    public partial class 商品信息修改 : Form
    {
        string strcon = denglu.strcon;
        DataSet myst = new DataSet();
        SqlDataAdapter myda;
        public 商品信息修改()
        {
            InitializeComponent();
        }

        private void btn_allselect_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            SqlConnection con = new SqlConnection(strcon);
            try
            {
                con.Open();
                string sqlSel = "select * from Goods,Supplier where Goods.gid=Supplier.gid and Goods.sid=Supplier.sid";
                SqlCommand com = new SqlCommand(sqlSel, con);
                myda = new SqlDataAdapter(com);
                myst.Tables.Clear();
                myda.Fill(myst, "Goods");
                dataGridView1.DataSource = myst.Tables["Goods"];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString() + "失败");
            }
        }

        private void btn_select_Click(object sender, EventArgs e)
        {
            string a = textBox1.Text;
            SqlConnection con = new SqlConnection(strcon);
            if (comboBox1.Text.Equals("供货商ID"))
            {
                try
                {
                    con.Open();
                    string sqlSel = "select * from Supplier inner join Goods on Supplier.gid=Supplier.gid where sid = '" + a + "'";
                    SqlCommand com = new SqlCommand(sqlSel, con);
                    myda = new SqlDataAdapter(com);
                    myst.Tables.Clear();
                    myda.Fill(myst, "Supplier");
                    dataGridView1.DataSource = myst.Tables["Supplier"];
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString() + "失败");
                }
                finally
                {
                    con.Close();
                }

                if (comboBox1.Text.Equals("货物ID"))
                {
                    try
                    {
                        con.Open();
                        string sqlSel = "select * from Goods inner join Supplier on Goods.gid=Supplier.gid where gid = '" + a + "'";
                        SqlCommand com = new SqlCommand(sqlSel, con);
                        myda = new SqlDataAdapter(com);
                        myst.Tables.Clear();
                        myda.Fill(myst, "Goods");
                        dataGridView1.DataSource = myst.Tables["Goods"];
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message.ToString() + "失败");
                    }
                    if (comboBox1.Text == "货物名称")
                    {
                        try
                        {
                            con.Open();
                            string sqlSel = "select * from Goods inner join Supplier on Goods.gid=Supplier.gid where gname = '" + a + "'";
                            SqlCommand com = new SqlCommand(sqlSel, con);
                            myda = new SqlDataAdapter(com);
                            myst.Tables.Clear();
                            myda.Fill(myst, "Goods");
                            dataGridView1.DataSource = myst.Tables["Goods"];
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message.ToString() + "失败");
                        }
                    }
                    if (comboBox1.Text == "供货商名称")
                    {
                        try
                        {
                            con.Open();
                            string sql = "select * from Supplier inner join Goods on Supplier.gid=Supplier.gid where sid = '" + a + "'";
                            SqlCommand com = new SqlCommand(sql, con);
                            myda = new SqlDataAdapter(com);
                            myst.Tables.Clear();
                            myda.Fill(myst, "Supplier");
                            dataGridView1.DataSource = myst.Tables["Supplier"];
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message.ToString() + "失败");
                        }
                    }
                }
            }
        }

        private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            DataGridViewRow tbxRow = dataGridView1.Rows[e.RowIndex];
            DataGridViewCellCollection dgvCC = tbxRow.Cells;
            lb_gid.Text = dgvCC[0].Value.ToString();
            tbx_gname.Text = dgvCC[1].Value.ToString();
            textBox2.Text = dgvCC[2].Value.ToString();
            textBox3.Text = dgvCC[3].Value.ToString();
            textBox4.Text = dgvCC[4].Value.ToString();
            textBox5.Text = dgvCC[5].Value.ToString();
            lb_sid.Text = dgvCC[6].Value.ToString();
            textBox6.Text = dgvCC[8].Value.ToString();
            textBox7.Text = dgvCC[9].Value.ToString();
            textBox8.Text = dgvCC[10].Value.ToString();
        }

        private void btn_update_Click(object sender, EventArgs e)
        {
            using (SqlConnection conn = new SqlConnection(denglu.strcon))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "update Goods set gname = '" + tbx_gname.Text + "', gtype = '" + textBox2.Text + "', gprice = '" + textBox3.Text + "', sprice = '" + textBox4.Text + "', gammount = '" + textBox5.Text + "' where gid='"+lb_gid.Text+"'";
                    int i=cmd.ExecuteNonQuery();
                    if (i==0)
                    {
                        MessageBox.Show("更新失败！");
                    }
                }
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "update Supplier set sname = '" + textBox6.Text + "', snumber = '" + textBox7.Text + "', saddress = '" + textBox8.Text + "' where sid='"+lb_sid.Text+"' and gid='"+lb_gid.Text+"'";
                    int i=cmd.ExecuteNonQuery();
                    if (i == 0)
                    {
                        MessageBox.Show("更新失败！");
                    }
                    else
                    {
                        MessageBox.Show("更新成功!");
                        btn_allselect_Click(null,null);
                    }
                
                }

            }
        }
    }
}
